/**
 * Copyright (c) 2011-2015, James Zhan 詹波 (jfinal@126.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package pro.cherish.knowledgeutil.common.path;


import lombok.extern.slf4j.Slf4j;
import pro.cherish.knowledgeutil.common.id.GUID;
import pro.cherish.knowledgeutil.date.DateUtilExt;

import java.io.File;

/**
 * new File("..\path\abc.txt") 中的三个方法获取路径的方法
 * 1： getPath() 获取相对路径，例如   ..\path\abc.txt
 * 2： getAbslutlyPath() 获取绝对路径，但可能包含 ".." 或 "." 字符，例如  D:\otherPath\..\path\abc.txt
 * 3： getCanonicalPath() 获取绝对路径，但不包含 ".." 或 "." 字符，例如  D:\path\abc.txt
 */
@Slf4j
public class PathUtilExt {

    private static String webRootPath;
    private static String rootClassPath;
    private static String contextPath = "";

    /**
     * 当前类字节码文件位置，dir
     *
     * @param clazz 类字节码对象
     * @return
     */
    public static String getPath(Class clazz) {
        String path = clazz.getResource("").getPath();
        return new File(path).getAbsolutePath();
    }

    /**
     * 获取对象所属类的字节码文件位置，dir
     *
     * @param object 目标对象
     * @return
     */
    public static String getPath(Object object) {
        String path = object.getClass().getResource("").getPath();
        return new File(path).getAbsolutePath();
    }


    /**
     * 获取rootClass 路径,即clases文件夹位置
     *
     * @return
     */
    public static String getRootClassPath() {
        if (rootClassPath == null) {
            try {
                String path = PathUtilExt.class.getClassLoader().getResource("").toURI().getPath();
                rootClassPath = new File(path).getAbsolutePath();
            } catch (Exception e) {
                String path = PathUtilExt.class.getClassLoader().getResource("").getPath();
                rootClassPath = new File(path).getAbsolutePath();
            }
        }
        return rootClassPath;
    }

    /**
     * 设置工具类rootClassPath
     *
     * @param rootClassPath 字节码文件根目录
     */
    public void setRootClassPath(String rootClassPath) {
        PathUtilExt.rootClassPath = rootClassPath;
    }


    /**
     * 获得目标对象所属类的包引用路径【reference路径】
     *
     * @param object 目标对象
     * @return
     */
    public static String getPackagePath(Object object) {
        Package p = object.getClass().getPackage();
        return p != null ? p.getName().replaceAll("\\.", "/") : "";
    }


    public static File getFileFromJar(String file) {
        throw new RuntimeException("Not finish. Do not use this method.");
    }

    /**
     * 获取 webRoot路径
     *
     * @return
     */
    public static String getWebRootPath() {
        if (webRootPath == null) {
            webRootPath = detectWebRootPath();
        }
        return webRootPath;
    }

    /**
     * 设置项目根目录
     *
     * @param webRootPath
     */
    public static void setWebRootPath(String webRootPath) {
        if (webRootPath == null) {
            return;
        }

        if (webRootPath.endsWith(File.separator)) {
            webRootPath = webRootPath.substring(0, webRootPath.length() - 1);
        }
        PathUtilExt.webRootPath = webRootPath;
    }

    /**
     * 获取项目根目录
     * fixme  在spring boot 中，无法访问未解压路径文件
     *
     * @return
     */
    private static String detectWebRootPath() {
        try {
            String path = PathUtilExt.class.getResource("/").toURI().getPath();
            return new File(path).getParentFile().getParentFile().getCanonicalPath();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取一个临时目录下32位guid的目录路径
     *
     * @return
     */
    public static String getTempDirectory() {
        return System.getProperty("java.io.tmpdir") + File.separator + GUID.newGUID() + File.separator;
    }

    /**
     * 获取临时目录
     *
     * @param directory
     * @return
     */
    public static String getTempDirectory(String directory) {
        return System.getProperty("java.io.tmpdir") + File.separator + directory + File.separator;
    }

    public static String getContextPath() {
        return contextPath;
    }

    public static void setContextPath(String contextPath) {
        PathUtilExt.contextPath = contextPath;
    }

    /**
     * 返回日期格式目录结构 ：2019分隔符11分割符01分割符
     *
     * @return
     */
    public static String getDateDirectory() {
        return DateUtilExt.getNowYear()
                + File.separator + DateUtilExt.getNowMonth()
                + File.separator + DateUtilExt.getNowDay() + File.separator;
    }

}


